<template>
  <div class="flex-1">
    <data-table ref="users" @page-change="fetch">
      <div slot="toolbar">
        <el-button type="primary" icon="plus" @click.native="createUser">创建</el-button>
      </div>
      <el-table :data="users" border height="100%">
        <el-table-column property="_id" label="ID" sortable min-width="120"></el-table-column>
        <el-table-column property="username" label="用户名" sortable min-width="120"></el-table-column>
        <el-table-column property="role" label="角色" min-width="90"></el-table-column>
        <el-table-column label="操作" align="center" width="120">
          <template slot-scope="scope">
            <el-button type="text" @click.native="deleteUser(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </data-table>
    <el-dialog title="创建用户" v-model="formVisible" @close="cancelForm">
      <el-form :model="form" :rules="rules" ref="form"
        :close-on-click-modal="false" :close-on-press-escape="false">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click.native="formVisible=false">取 消</el-button>
        <el-button type="primary" @click.native="saveForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import DataTable from '@/components/DataTable'
import { user as userRes } from '@/resources'
export default {
  data () {
    return {
      search: {
      },
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [{
          required: true, message: '请输入用户名', trigger: 'blur'
        }],
        password: [{
          required: true, message: '请输入密码', trigger: 'blur'
        }]
      },
      formVisible: false,
      users: []
    }
  },
  components: {
    DataTable
  },
  methods: {
    fetch (current = 1) {
      this.$refs.users.query(userRes, current, { search: this.search }).then(list => {
        this.users = list
      }).catch(err => {
        console.error(err)
      })
    },
    createUser () {
      console.log('123')
      this.formVisible = true
    },
    cancelForm () {
      this.form.username = ''
      this.form.password = ''
      this.formVisible = false
    },
    saveForm () {
      this.$refs.form.validate(valid => {
        if (valid) {
          userRes.save(null, this.form).then(() => {
            this.cancelForm()
            this.$message({
              type: 'success',
              message: '新增成功'
            })
            this.fetch()
          }).catch((err) => {
            this.$message({
              type: 'error',
              message: err.status === 422 ? '用户已存在' : '新增失败'
            })
          })
        }
      })
    },
    deleteUser (user) {
      this.$confirm(`This action will remove the selected user: ${user.username} forever, still going on?`, '提示', {
        type: 'warning'
      }).then(() => {
        userRes.delete({ _id: user._id }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          this.fetch()
        })
      }).catch(() => {})
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.fetch()
    })
  }
}
</script>
